package cn.enilu.flash.service;

import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.net.URISyntaxException;

/**
 * @author qiang.lin
 * @ClassName FileOperator.java
 * @Description TODO
 * @createTime 2021年08月04日 22:05:00
 */
@Component
@Slf4j
public class FileOperator {

    @Autowired
    private COSClient cosClient;

    @Value("${txcloud.bucketName}")
    private String bucketName;

    /**
     * 上传文件
     *
     * @param file
     */
    public void uplodFile(File file) {
        PutObjectResult objectResult = cosClient.putObject(bucketName, file.getName(), file);
        log.info("上传文件{}的大小{}结果是：{}", file.getName(), file.length(), objectResult.toString());
    }


    /**
     * 获取文件路径
     *
     * @return
     */
    public String getFilePath(String fileName) {
        try {
            return cosClient.getObjectUrl(bucketName, fileName).toURI().toString();
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return null;
        }
    }
}
